package me.principality.webapp.controller

import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import me.principality.webapp.domain.repository.CoffeeRepository
import me.principality.webapp.response.VueDataFormat
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException

@Api(tags= arrayOf("商品管理"))
@RestController
@RequestMapping("/api/coffee")
class CoffeeController(private val repository: CoffeeRepository) {
    @ApiOperation("商品列表")
    @GetMapping("/")
    @VueDataFormat
    fun findAll() = repository.findAllByOrderByOriginCountryDesc()

    @ApiOperation("商品详情")
    @GetMapping("/{name}")
    @VueDataFormat
    fun findOne(@PathVariable name: String) =
        repository.findByProductName(name) ?: throw ResponseStatusException(
            HttpStatus.NOT_FOUND,
            "This coffee does not exist"
        )
}
